import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

/* Layout */
import Layout from '@/layout';

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

/**
 *
 * hidden: true                   当设置 true 的时候该路由不会再侧边栏出现 如401，login，编辑等页面（default is false）
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes user mode, otherwise not show the root menu
 * redirect:                      noRedirect 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'             设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * meta : {
    roles: ['admin','editor']     设置该路由进入的权限，支持多个权限叠加
    title: 'title'                设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'              设置该路由的图标
    breadcrumb: false             如果设置为false，则不会在breadcrumb面包屑中显示
    activeMenu: '/example/list'   if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * 代表那些不需要动态判断权限的路由，如登录页、404、等通用页面
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/demo01',
    name: 'demo01',
    component: () => import('@/views/test/demo01'),
    hidden: true,
    meta: {
      title: '饼图pie',
      noCache: true
    }
  },
  {
    path: '/demo02',
    name: 'demo02',
    component: () => import('@/views/test/demo02'),
    hidden: true,
    meta: {
      title: '折线图',
      noCache: true
    }
  },
  {
    path: '/demo03',
    name: 'demo03',
    component: () => import('@/views/test/demo03'),
    hidden: true,
    meta: {
      title: '折线图',
      noCache: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: {
          title: '首页',
          icon: 'dashboard',
          noCache: true,
          affix: true
        }
      },
      {
        path: '/register-data',
        component: () => import('@/views/system/data/registerData'),
        name: 'registerData',
        meta: {title: '系统注册数据', noCache: true, activeMenu: '/dashboard'},
        hidden: true
      },
      {
        path: '/visitor-data',
        component: () => import('@/views/system/data/visitorData'),
        name: 'visitorData',
        meta: {title: '系统访问数据', noCache: true, activeMenu: '/dashboard'},
        hidden: true
      }
    ]
  }
];

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/system',
    component: Layout,
    redirect: '/system/user',
    name: 'system',
    meta: {
      title: '系统相关',
      icon: 'example'
    },
    children: [
      {
        path: '/user-manage',
        name: 'userManage',
        component: () => import('@/views/system/user/userManage'),
        meta: {
          noCache: true,
          title: '后台用户管理'
        }
      },
      {
        path: '/role-manage',
        name: 'roleManage',
        component: () => import('@/views/system/role/roleManage'),
        meta: {
          noCache: true,
          title: '角色权限管理'
        }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/user-basic-info',
    name: 'user',
    meta: {
      title: '用户相关',
      icon: 'user'
    },
    children: [
      {
        path: '/user-basic-info',
        component: () => import('@/views/user/basic/userBasicInfo'), // Parent router-view
        name: 'userBasicInfo',
        meta: {
          noCache: true,
          title: '用户基本信息'
        }
      },
      {
        path: '/bank-card-manage',
        name: 'bankCardManage',
        component: () => import('@/views/user/bankcard/bankCardManage'),
        meta: {
          noCache: true,
          title: '银行卡管理'
        }
      },
      {
        path: '/emp-basic-info-register',
        name: 'empBasicInfoRegister',
        component: () => import('@/views/user/emp/empBasicInfoRegister'),
        meta: {
          noCache: true,
          title: '员工基本信息登记'
        }
      }
    ]
  },
  {
    path: '/finance',
    component: Layout,
    redirect: '/finance/salaryDetails',
    name: 'finance',
    meta: {
      title: '财务相关',
      icon: 'finance'
    },
    children: [
      {
        path: '/salary-details',
        name: 'salaryDetails',
        component: () => import('@/views/finance/salary/salaryDetails'),
        meta: {
          noCache: true,
          title: '工资明细'
        }
      },
      {
        path: '/borrowing-manage',
        name: 'borrowingManage',
        component: () => import('@/views/finance/borrowing/borrowingManage'),
        meta: {
          noCache: true,
          title: '借支管理'
        }
      },
      {
        path: '/withdraw-manage',
        name: 'withdrawManage',
        component: () => import('@/views/finance/withdraw/withdrawManage'),
        meta: {
          noCache: true,
          title: '提现管理'
        }
      },
      {
        path: '/registfee-manage',
        name: 'registfeeManage',
        component: () => import('@/views/finance/registfee/registfeeManage'),
        meta: {
          noCache: true,
          title: '注册费管理'
        }
      },
      {
        path: '/empreturn-fee',
        name: 'empReturnFee',
        component: () => import('@/views/finance/returnfee/empReturnFee'),
        meta: {
          noCache: true,
          title: '员工返费管理'
        }
      },
      {
        path: '/threelevel-distribute',
        name: 'threeLevelDistribute',
        component: () => import('@/views/finance/threeleveldistribute/threeLevelDistribute'),
        meta: {
          noCache: true,
          title: '三级分销获利管理'
        }
      },
      {
        path: '/dismissal-wage',
        name: 'dismissalWage',
        component: () => import('@/views/finance/dismissal/dismissalWage'),
        meta: {
          noCache: true,
          title: '离职工资管理'
        }
      },
      {
        path: '/score-award',
        name: 'scoreAward',
        component: () => import('@/views/finance/scoreaward/scoreAward'),
        meta: {
          noCache: true,
          title: '积分奖励'
        }
      },
      {
        path: '/daily-wage',
        name: 'dailyWage',
        component: () => import('@/views/finance/daily/dailyWage'),
        meta: {
          noCache: true,
          title: '日日薪管理'
        }
      },
      {
        path: '/finance-data',
        component: () => import('@/views/system/data/financeData'),
        name: 'financeData',
        meta: {
          title: '系统财务数据',
          noCache: true,
          activeMenu: '/dashboard'
        },
        hidden: true
      }
    ]
  },
  {
    path: '/recruit',
    component: Layout,
    redirect: '/recruit/companyManage',
    name: 'recruit',
    meta: {
      title: '招工相关',
      icon: 'recruit'
    },
    children: [
      {
        path: '/company-manage',
        name: 'companyManage',
        component: () => import('@/views/recruit/company/companyManage'),
        meta: {
          noCache: true,
          title: '企业管理'
        }
      },
      {
        path: '/resident-manage',
        name: 'residentManage',
        component: () => import('@/views/recruit/resident/residentManage'),
        meta: {
          noCache: true,
          title: '驻厂管理'
        }
      },
      {
        path: '/factory',
        name: 'factory',
        component: () => import('@/views/recruit/factory/factory'),
        meta: {
          noCache: true,
          title: '企业类型'
        }
      },
      {
        path: '/job',
        name: 'job',
        component: () => import('@/views/recruit/job/job'),
        meta: {
          noCache: true,
          title: '岗位类型'
        }
      },
      {
        path: '/workinfo',
        name: 'workinfo',
        component: () => import('@/views/recruit/work/workinfo'),
        meta: {
          noCache: true,
          title: '招聘信息'
        }
      },
    ]
  },
  {
    path: '/customer',
    component: Layout,
    redirect: '/customer/applyManage',
    alwaysShow: true,
    name: 'customer',
    meta: {
      title: '客服相关',
      icon: 'customer'
    },
    children: [
      {
        path: '/apply-manage',
        name: 'applyManage',
        component: () => import('@/views/customer/apply/applyManage'),
        meta: {
          noCache: true,
          title: '报名管理'
        }
      },
      {
        path: '/common-issue',
        name: 'commonIssue',
        component: () => import('@/views/customer/common/commonIssue'),
        meta: {
          noCache: true,
          title: '常见问题'
        }
      },
      {
        path: '/customer-reply',
        name: 'customerReply',
        component: () => import('@/views/customer/reply/customerReply'),
        meta: {
          noCache: true,
          title: '客服回复'
        }
      },
      {
        path: '/service-issue',
        name: 'serviceIssue',
        component: () => import('@/views/customer/service/serviceIssue'),
        meta: {
          noCache: true,
          title: '客服问题'
        }
      },
      {
        path: '/message',
        name: 'message',
        component: () => import('@/views/customer/message/message'),
        meta: {
          noCache: true,
          title: '系统消息'
        }
      },
      {
        path: '/quick-applyjob',
        name: 'quickApplyJob',
        component: () => import('@/views/customer/quick/quickApplyJob'),
        meta: {
          noCache: true,
          title: '快速求职'
        }
      }
    ]
  },
  {
    path: '/website',
    component: Layout,
    redirect: '/website/webSiteNewsManage',
    alwaysShow: true,
    name: 'website',
    meta: {
      title: '官网相关',
      icon: 'website'
    },
    children: [
      {
        path: '/website-news-manage',
        name: 'webSiteNewsManage',
        component: () => import('@/views/website/news/webSiteNewsManage'),
        meta: {
          noCache: true,
          title: '官网新闻'
        }
      }
    ]
  },
  {
    path: '/store',
    component: Layout,
    redirect: '/store/storeManage',
    alwaysShow: true,
    name: 'store',
    meta: {
      title: '门店相关',
      icon: 'store'
    },
    children: [
      {
        path: '/store-manage',
        name: 'storeManage',
        component: () => import('@/views/store/store/storeManage'),
        meta: {
          noCache: true,
          title: '店铺管理'
        }
      },
      {
        path: '/salesclerk',
        name: 'salesclerk',
        component: () => import('@/views/store/salesclerk/salesclerk'),
        meta: {
          noCache: true,
          title: '店员管理'
        }
      }
    ]
  },
  {
    path: '/shopping',
    component: Layout,
    redirect: '/shopping/commodity',
    alwaysShow: true,
    name: 'shopping',
    meta: {
      title: '商城相关',
      icon: 'shopping'
    },
    children: [
      {
        path: '/commodity',
        name: 'commodity',
        component: () => import('@/views/shopping/commodity/commodity'),
        meta: {
          noCache: true,
          title: '商品管理'
        }
      },
      {
        path: '/order',
        name: 'order',
        component: () => import('@/views/shopping/order/order'),
        meta: {
          noCache: true,
          title: '订单管理'
        }
      },
      {
        path: '/inventory',
        name: 'inventory',
        component: () => import('@/views/shopping/inventory/inventory'),
        meta: {
          noCache: true,
          title: '库存管理'
        }
      }
    ]
  },
  {
    path: '/personnel',
    component: Layout,
    redirect: '/personnel/dailyReport',
    alwaysShow: true,
    name: 'personnel',
    meta: {
      title: '人事相关',
      icon: 'personnel'
    },
    children: [
      {
        path: '/daily-report',
        name: 'dailyReport',
        component: () => import('@/views/personnel/report/dailyReport'),
        meta: {
          noCache: true,
          title: '日输送报表'
        }
      }
    ]
  },
  {
    path: '/social',
    component: Layout,
    redirect: '/social/classifyManage',
    alwaysShow: true,
    name: 'social',
    meta: {
      title: '社交相关',
      icon: 'social'
    },
    children: [
      {
        path: '/classify-manage',
        name: 'classifyManage',
        component: () => import('@/views/social/classify/classifyManage'),
        meta: {
          noCache: true,
          title: '分类管理'
        }
      },
      {
        path: '/report-manage',
        name: 'reportManage',
        component: () => import('@/views/social/report/reportManage'),
        meta: {
          noCache: true,
          title: '举报管理'
        }
      },
      {
        path: '/comment-manage',
        name: 'commentManage',
        component: () => import('@/views/social/comment/commentManage'),
        meta: {
          noCache: true,
          title: '评论管理'
        }
      },
      // {
      //   path: '/business-comment',
      //   name: 'businessComment',
      //   component: () => import('@/views/social/business/businessComment'),
      //   meta: {
      //     noCache: true,
      //     title: '企业点评管理'
      //   }
      // },
      {
        path: '/circle-manage',
        name: 'circleManage',
        component: () => import('@/views/social/circle/circleManage'),
        meta: {
          noCache: true,
          title: '圈子管理'
        }
      },
      // {
      //   path: '/divulge-salary',
      //   name: 'divulgeSalary',
      //   component: () => import('@/views/social/divulge/divulgeSalary'),
      //   meta: {
      //     noCache: true,
      //     title: '晒工资管理'
      //   }
      // },
      {
        path: '/video-manage',
        name: 'videoManage',
        component: () => import('@/views/social/video/videoManage'),
        meta: {
          noCache: true,
          title: '视频管理'
        }
      },
      {
        path: '/banned-word',
        name: 'bannedWord',
        component: () => import('@/views/social/banned/bannedWord'),
        meta: {
          noCache: true,
          title: '违禁字管理'
        }
      },
      {
        path: '/news-manage',
        name: 'newsManage',
        component: () => import('@/views/social/news/newsManage'),
        meta: {
          noCache: true,
          title: '新闻管理'
        }
      }
    ]
  },
  {
    path: '/others',
    component: Layout,
    redirect: '/others/activityManage',
    alwaysShow: true,
    name: 'others',
    meta: {
      title: '其他',
      icon: 'others'
    },
    children: [
      {
        path: '/activity-manage',
        name: 'activityManage',
        component: () => import('@/views/others/activity/activityManage'),
        meta: {
          noCache: true,
          title: '活动管理'
        }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  {path: '*', name: '404', redirect: '/404', hidden: true}
];

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({y: 0}),
  routes: constantRoutes,
  base: 'lanpin_cms'
});

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher // reset router
}

export default router
